/**
 * Created Date: Saturday, July 22nd 2023, 3:16:49 pm
 * Author: CodingGorit
 * -----
 * Last Modified: Mon Jul 24 2023
 * Modified By: CodingGorit
 * -----
 * Copyright © 2019 —— 2023 fmin-courses TP Center All Rights Reserved
 * ------------------------------------
 * Javascript will save your soul!
 */

const express = require('express');
import { uploads } from '../../../../../common/MulterConfig';
import { IRouterConf } from "../../../..";
import { Request, Response, NextFunction } from 'express';
import log from "../../../../../utils/log";
import Utils from '../../../../../utils/Utils';
import { ResultCodeEnum, ResultMessageEnum } from "../../../../../enums/ResultEmums";
import { IResponse } from '../../../../../types';
import { uploadFileByQiuniuOSS } from '../../../../../service/OssService';
const TAG = "MailController";

const file = express.Router();

/**
 * upload.single 用于接收一个文件,name 与 avatar 对应, 文件上传到本地
 * 
 */
file.post('/profile', uploads.single('avatar'), function (req: Request, res: Response, next: NextFunction) {
    if (!req.file) {
        res.send("<h3>file 内容为空</h3>");
    } else {
        log.info(`${TAG} upload file, call ${Utils.getFileSufficName(req.file?.filename)}`);
        log.info(`${TAG}, ${JSON.stringify(req.file)}`);
        res.send({
            code: ResultCodeEnum.UPLOAD_SUCCESS,
            msg: ResultMessageEnum.UPLOAD_SUCCESS,
            data: ""
        });
    }
    // log.info(`${TAG}`);
    // req.file is the `avatar` file
    // req.body will hold the text fields, if there were any
});

/**
 *  use form like this to upload file to qiniu-oss
 *  <form action="http://localhost:1347/api/v1/tools/file-service/upload/qiniu" method="post" enctype="multipart/form-data">
        <div class="file-container">
            <input type="file" name="file" />
            <input type="submit"/>
        </div>
    </form>
 */
file.post('/upload/qiniu', uploads.single('file'), function (req: Request, res: IResponse, next) {
    log.info(`${TAG} upload file`);
    uploadFileByQiuniuOSS(req, res);
});

const routes: Array<IRouterConf> = [{
    path: "/file-service",
    router: file
}];

export = routes;
